//
//  NewCoureseViewController.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/30.
//  Copyright © 2016年 Gcx. All rights reserved.
//
#define REDCOLOR [self getColor:@"e86666"]


#import "NewCoureseViewController.h"
#import "SC_TextFeild.h"
#import "SCTypeBtn.h"
#import "MMLabel.h"

@interface NewCoureseViewController(){

    SC_TextFeild *_classNameTF;
    SC_TextFeild *_classTimeTF;
    
    SCTypeBtn    *_succeedBtn;
}

@end

@implementation NewCoureseViewController




- (void)viewDidLoad{

    [super viewDidLoad];
    
    [self.navbar setTitle:@"新增课程"];
    
    [self getNewSystemTime];
    
    [self creatMainView];
}

- (void)creatMainView{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SIZEWIDTH, ScaleSize(336))];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIView *xiahuaxianView = [[UIView alloc]initWithFrame:CGRectMake(ScaleSize(42), ScaleSize(168), SIZEWIDTH - ScaleSize(42)*2, 0.5)];
    xiahuaxianView.backgroundColor = colordbdbdb;
    [view addSubview:xiahuaxianView];
    
    MMLabel *nameLabel = [[MMLabel alloc]initWithFrame:CGRectMake(ScaleSize(32), ScaleSize(63), 75, ScaleSize(42)) ];
    
    
    nameLabel.text =@"* 课程名称:";
    nameLabel.font = NormalFont(42);
    nameLabel.textColor = REDCOLOR;
    nameLabel.keyWord = @"课程名称:";
    nameLabel.keyWordColor =Color333333;
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScaleSize(42), CGRectGetMaxY(xiahuaxianView.frame)+ScaleSize(63), 75, ScaleSize(42))];
    timeLabel.text = @" 上课学期:";
    timeLabel.font = NormalFont(42);
    timeLabel.textColor = Color333333;
    
    [view addSubview:nameLabel];
    [view addSubview:timeLabel];
    
    
    _classNameTF = [[SC_TextFeild alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame)+2, ScaleSize(63), SIZEWIDTH - 90, ScaleSize(42))];
    _classTimeTF = [[SC_TextFeild alloc]initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame), CGRectGetMaxY(xiahuaxianView.frame)+ScaleSize(63), SIZEWIDTH - 90, ScaleSize(42))];
    
    [_classNameTF setValue:Color333333 forKeyPath:@"_placeholderLabel.textColor"];
    [_classTimeTF setValue:Color333333 forKeyPath:@"_placeholderLabel.textColor"];
    
    _classTimeTF.userInteractionEnabled = NO;

    
    [view addSubview:_classTimeTF];
    [view addSubview:_classNameTF];
    
    _succeedBtn = [[SCTypeBtn alloc]initWithFrame:CGRectMake(ScaleSize(60), CGRectGetMaxY(view.frame) + ScaleSize(96), SIZEWIDTH - ScaleSize(120), ScaleSize(140))];
    [_succeedBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.view addSubview:_succeedBtn];
    
    _succeedBtn.isCanUse = NO;
    
    [_succeedBtn addTarget:self action:@selector(loginBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_succeedBtn];

    
    _classTimeTF.myBlock = ^(NSString *str){
    
        if (_classTimeTF.text.length > 0 && _classNameTF.text.length > 0) {
            
            _succeedBtn.isCanUse = YES;
        
        } else{
        
            _succeedBtn.isCanUse = NO;
        }
    };
    
    _classNameTF.myBlock = ^(NSString *str){
        
        if (_classTimeTF.text.length > 0 && _classNameTF.text.length > 0) {
            
            _succeedBtn.isCanUse = YES;
            
        } else{
            
            _succeedBtn.isCanUse = NO;
        }
    };
}

- (void)loginBtnClicked{
    
    
    NSArray *array = [_classNameTF.text componentsSeparatedByString:@" "]; //从字符A中分隔成2个元素的数组
    
    NSLog(@"array == %ld", array.count);
    
    if (_classNameTF.text.length > 50){
        
        [[JLActionManager sharedInstance]showHUDWithLabelText:@"课程名称不能超过50位" imageView:FailImage inView:self.view];
        
    } else {
        
        [self loadDataCreatNewCourse];
    }
}

- (void)loadDataCreatNewCourse{

    NSDictionary *dic = @{@"oauth_token":@"815bffb68e2ea44e38cf16a98a5a29a4",@"oauth_token_secret":@"8e84bc64f03d73b5c75406ef1d7349f7"};

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];
    
    [params setObject:_classNameTF.text forKey:@"kcmc"];
    
    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_createCourse params:params];
    
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        if ([resp.originalDictionary[@"status"]integerValue] == 0) {
            
            POST_SUCCEED(resp.originalDictionary[@"statusCode"]);
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                self.myBlock();
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        } else {
            
            POST_FAIL(resp.originalDictionary[@"statusCode"]);
            
        }
        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
    }];

}


-(UIColor *)getColor:(NSString *)hexColor {
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green / 255.0f) blue:(float)(blue / 255.0f) alpha:1.0f];
}


#pragma mark -- 获取当前学期

- (void)getNewSystemTime{

    NSDictionary *dic = @{@"oauth_token":@"815bffb68e2ea44e38cf16a98a5a29a4",@"oauth_token_secret":@"8e84bc64f03d73b5c75406ef1d7349f7"};
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];

    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_getNowXueqi params:params];
    
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        if ([resp.originalDictionary[@"status"]integerValue] == 0) {
            
            _classTimeTF.text = resp.originalDictionary[@"data"][@"skxq"];

        } else {
            
            POST_FAIL(resp.originalDictionary[@"statusCode"]);

        }
        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
    }];
}

@end
